package com.liuche.pictureserver.aop;

import com.liuche.pictureserver.aop.annotation.Login;
import com.liuche.pictureserver.common.exception.BizException;
import com.liuche.pictureserver.common.exception.ErrorCode;
import com.liuche.pictureserver.pojo.dto.UserHolderDTO;
import com.liuche.pictureserver.service.IUserService;
import com.liuche.pictureserver.utils.UserHolder;
import jakarta.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * 用户权限检查
 *
 * @author liuche
 */
@Aspect
@Component
public class LoginInterceptor {

    @Resource
    private IUserService userService;

    /**
     * 执行拦截
     *
     * @param joinPoint 切入点
     * @param login     登录校验注解
     */
    @Around("@annotation(login)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, Login login) throws Throwable {
        // 当前登录用户
        UserHolderDTO userInfo = UserHolder.getUserInfo();
        if (ObjectUtils.isEmpty(userInfo)) {
            throw new BizException(ErrorCode.NOT_LOGIN_ERROR);
        }
        // 通过权限校验，放行
        return joinPoint.proceed();
    }
}
